<template>
  <div class="common-warp wh100 warp-bg">
    <div class="noo">
      <a-card title="基本信息">
        <a-form ref="formRef" :model="state.forma" :rules=rules>
          <a-form-item label="基本名称" name="name">
            <a-input v-model:value="state.forma.name" />
          </a-form-item>
          <a-form-item label="是否跨天" name="isCors">
            <a-radio-group :options="plainOptions" v-model:value="state.forma.isCors" />
          </a-form-item>
        </a-form>
      </a-card>

      <a-card title="时间配置" style="margin-top: 12px;margin-bottom: 12px;">
        <a-form ref="formRef" :model="state.formb" :rules=rules>
          <a-form-item label="最早签到" name="inStart">
            <a-time-picker v-model:value="state.formb.inStart" valueFormat="HH:mm:ss" style="width: 100%;" />
          </a-form-item>
          <a-form-item label="签到时间" name="inTime">
            <a-time-picker v-model:value="state.formb.inTime" valueFormat="HH:mm:ss" style="width: 100%;" />
          </a-form-item>
          <a-form-item label="最晚签到" name="inEnd">
            <a-time-picker v-model:value="state.formb.inEnd" valueFormat="HH:mm:ss" style="width: 100%;" />
          </a-form-item>
          <a-form-item label="最早签退" name="outStart">
            <a-time-picker v-model:value="state.formb.outStart" valueFormat="HH:mm:ss" style="width: 100%;" />
          </a-form-item>
          <a-form-item label="最早签退" name="outTime">
            <a-time-picker v-model:value="state.formb.outTime" valueFormat="HH:mm:ss" style="width: 100%;" />
          </a-form-item>
          <a-form-item label="最晚签退" name="outEnd">
            <a-time-picker v-model:value="state.formb.outEnd" valueFormat="HH:mm:ss" style="width: 100%;" />
          </a-form-item>
        </a-form>
      </a-card>
    </div>
    <a-button type="primary" block @click="confirm(formRef)">提交</a-button>
  </div>
</template>

<script lang='ts' setup>
import { ref, reactive, onMounted, } from 'vue';
import { useAttendRuleCreateApi, useAttendRuleQueryApi, useAttendRuleUpdateApi } from '@/api/index'
import { message } from 'ant-design-vue';
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute()

const formRef = ref();

const plainOptions = [
  { label: '是', value: 1 },
  { label: '否', value: 2 },
];

const rules = {
  name: [{ required: true, message: '请输入分组名称', trigger: 'change' }],
};

const useContenta = () => {
  return {
    isCors: 2,
    name: "",
    code: ""
  }
}
const useContentb = () => {
  return {
    outEnd: "08:00:00",
    outStart: "09:00:00",
    outTime: "10:00:00",
    inEnd: "17:00:00",
    inStart: "18:00:00",
    inTime: "19:00:00"
  }
}

const state = reactive({
  forma: useContenta(),
  formb: useContentb()
})

//根据路由参数搜索
const queryData = async (casbinCode: string) => {
  let { data: { code, name, isCors, inEnd, inStart, inTime, outEnd, outStart, outTime } } = await useAttendRuleQueryApi({ code: casbinCode })
  state.forma = {
    code,
    name,
    isCors
  }
  state.formb = {
    inEnd, inStart, inTime, outEnd, outStart, outTime
  }
}

//提交
const confirm = (formRef: any) => {
  if (!formRef) return;
  formRef.validate().then(async () => {
    await useAttendRuleUpdateApi({ ...state.forma, ...state.formb })
    message.success("操作成功", 2);
    setTimeout(() => {
      router.push({ name: 'AttentRule' })
    }, 2300)
  })
};

onMounted(() => {
  document.title = '编辑'
  let { name, code }: any = route.query
  state.forma.name = name
  state.forma.code = code
  queryData(code)
})

</script>
<style scoped lang='less'>
.noo {
  height: calc(100vh - 56px);
  overflow-y: scroll;

  :deep(.ant-form-item-label) {
    padding: 0 !important;
  }

  :deep(.ant-card-body) {
    padding: 12px !important;
  }

  :deep(.ant-card-head) {
    padding: 12px !important;
  }

  :deep(.ant-form-item) {
    margin-bottom: 12px !important;
  }

}
</style>